//
//  TrueCustomerCell.m
//  WGFGlobalHouses
//
//  Created by waiguofang on 15/5/20.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#define KEY_WINDOW  [[UIApplication sharedApplication]keyWindow]

#import "TrueCustomerCell.h"

@interface TrueCustomerCell()
@property (weak, nonatomic) IBOutlet UIView *moveView;
@property (weak, nonatomic) IBOutlet UIView *btnBackView;

@property (nonatomic,assign)CGPoint startPoint;
@property(nonatomic,assign) CGFloat lastXpox;

@property(nonatomic,assign) NSInteger moveLeft;


@end

@implementation TrueCustomerCell
@synthesize startPoint,lastXpox;

- (void)awakeFromNib {
    
    
}
CGPoint originalCenter ;
-(void)layoutSubviews
{
    [super layoutSubviews];
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];
    originalCenter  =self.moveView.center;
    [self.moveView addGestureRecognizer:pan];
}



-(void)panGesture:(UIPanGestureRecognizer *)recognizer
{
    CGPoint touchPoint = [recognizer locationInView:self.contentView];
    
//
//    
//    if(pan.state == UIGestureRecognizerStateBegan)
//    {
//        startPoint = touchPoint;
//    }else
//    {
//        
////            center.x += translation.x;
////            pan.view.center = center;
////            [pan setTranslation:CGPointZero inView:pan.view];
//       
//        
//           
//       
//        
//        
//       
//    }
    
    
    
    if (recognizer.state == UIGestureRecognizerStateChanged) {
        
        [self moveViewWithX:touchPoint.x - startPoint.x];
        
    }else if (recognizer.state == UIGestureRecognizerStateBegan){
        
         startPoint = touchPoint;
        
    }else if (recognizer.state == UIGestureRecognizerStateCancelled){
        
        [UIView animateWithDuration:0.3 animations:^{
            
            [self moveViewWithX:0];
            
        } completion:^(BOOL finished) {
            
//            if ([delegate respondsToSelector:@selector(MenuDidHide:)]) {
//                [delegate MenuDidHide:self];
//            }
            
            lastXpox = self.moveView.frame.origin.x;
            
        }];
    }else if (recognizer.state == UIGestureRecognizerStateEnded){
        
        if (touchPoint.x - startPoint.x > 0)
        {
            self.moveLeft = 1;
            [UIView animateWithDuration:0.3 animations:^{
                [self moveViewWithX:60];
                
            } completion:^(BOOL finished) {
                
//                if ([delegate respondsToSelector:@selector(MenuDidHide:)]) {
//                    [delegate MenuDidHide:self];
//                }
                
                lastXpox = self.moveView.frame.origin.x;
                
            }];
        }
        else
        {
            [UIView animateWithDuration:0.3 animations:^{
                self.moveLeft = -1;
                [self moveViewWithX:-self.btnBackView.frame.size.width];
            } completion:^(BOOL finished) {
                
//                if ([delegate respondsToSelector:@selector(menuDidShow:)]) {
//                    [delegate menuDidShow:self];
//                }
                
                lastXpox = self.moveView.frame.origin.x;
                
            }];
            
        }
    }

    
    
   
}

- (void)moveViewWithX:(float)x
{
    
    if (x!=0) {
        x+=lastXpox;
    }
    if (self.moveLeft == 1)
    {
        x = x>60?60:x;
        x = x<-60 ?-60:x;
        self.moveLeft = 0;
    }
    else
    {
        x = x>160?160:x;
        x = x<-self.btnBackView.frame.size.width ?-self.btnBackView.frame.size.width:x;
        self.moveLeft = 0;
    }
    
    
    
    CGRect buf = self.moveView.frame;
    buf.origin.x = x;
    [self.moveView setFrame:buf];
    
}

@end
